//
//  TLTweetCell.m
//  Coding_iOS
//
//  Created by taolei-mac on 2017/11/23.
//  Copyright © 2017年 com.lianchuan. All rights reserved.
//

#import "TLTweetCell.h"
#import "TLTweetDetailView.h"
#import "TLCommentsAndLikesView.h"
#import "TLTweetToolBar.h"
#import "TLTweetFrame.h"
#import "TLTweet.h"

@interface TLTweetCell()

@property (nonatomic, weak) TLTweetDetailView *tweetDetailView;

@property (nonatomic, weak) TLCommentsAndLikesView *commentsView;

@property (nonatomic, weak) TLTweetToolBar *toolBar;

@property (nonatomic, weak) UIImageView *bottomLine;

@end

@implementation TLTweetCell

- (void)setTweetFrame:(TLTweetFrame *)tweetFrame
{
    _tweetFrame = tweetFrame;
    self.tweetDetailView.detailFrame = tweetFrame.tweetDetailViewFrame;
    
    self.toolBar.frame = tweetFrame.toolbarFrame;
    self.toolBar.tweet = tweetFrame.tweet;
    
    CGFloat bottomLineY = 0;
    if(tweetFrame.tweet.likes > 0){
        
        self.commentsView.hidden = NO;
        self.commentsView.commentsFrame = tweetFrame.commentsFrame;
        bottomLineY = CGRectGetMaxY(self.commentsView.frame);
    }else{
        
        self.commentsView.hidden = YES;
        bottomLineY = CGRectGetMaxY(self.toolBar.frame);
    }
    
    self.bottomLine.frame = CGRectMake(0, bottomLineY, TLScreenW, 1.0);
}

+ (instancetype)createTweetCellWithTableView:(UITableView *)tableView
{
    static NSString *Id = @"tweet";
    TLTweetCell *cell = [tableView dequeueReusableCellWithIdentifier:Id];
    if(!cell){
        
        cell = [[TLTweetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Id];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        
        // 1. 添加冒泡详情
        TLTweetDetailView *tweetDetailView = [[TLTweetDetailView alloc] init];
        tweetDetailView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:tweetDetailView];
        self.tweetDetailView = tweetDetailView;
        
        // 2. 添加工具条
        TLTweetToolBar *toolBar = [[TLTweetToolBar alloc] init];
        [self.contentView addSubview:toolBar];
        self.toolBar = toolBar;
        
        // 3. 添加评论
        TLCommentsAndLikesView *commentsView = [[TLCommentsAndLikesView alloc] init];
        commentsView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:commentsView];
        self.commentsView = commentsView;
        
        // 4. 底部分割线
        UIImageView *bottomLine = [[UIImageView alloc] initWithImage:[UIImage originImageWithName:@"splitlineImg"]];
        [self addSubview:bottomLine];
        self.bottomLine = bottomLine;
        
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

@end
